package cn.itsource.param.service.impl;



import cn.hutool.core.date.DateTime;
import cn.itsource.param.domain.Param;
import cn.itsource.param.mapper.ParamMapper;
import cn.itsource.param.query.ParamQuery;
import cn.itsource.param.service.IParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ParamServiceImpl implements IParamService {
    @Autowired
    private ParamMapper paramMapper;
    @Override
    public Map<String, Object> param() {
        //获取所有的参数
      List<Param> list = paramMapper.selectAll();
        return list.stream()
                .collect(Collectors.toMap(Param::getParamKey,Param::getParamValue));
    }

    @Override
    @Transactional
    public void save(Param param) {
        if(param.getId() == null){
            paramMapper.insert(param);
        }else {
            param.setUpdateTime(DateTime.now());
            paramMapper.updateByPrimaryKeySelective(param);
        }
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Long id) {
        return paramMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Param selectByPrimaryKey(Long id) {
        return paramMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Param> pageInfo(ParamQuery paramQuery) {
        PageHelper.startPage(paramQuery.getCurrentPage(),paramQuery.getPageSize());
        List<Param> params = paramMapper.selectAllByQuery(paramQuery);
        return new PageInfo<Param>(params);
    }

    @Override
    @Transactional
    public void deleteByCollection(List<Param> params) {
        paramMapper.deleteByConllection(params);
    }

   /* @Override
    public Boolean exist(Param artileType) {
        //根据用户名去数据库查询用户对象
        Param artileTypeByDB = paramMapper.getByName(artileType.getTypeName());
        if (artileType.getId() == null) {
            return artileTypeByDB == null;
        }else{
            //根据用户名去数据库查询
            if (artileTypeByDB == null) {
                return true;
            }else{
                return artileTypeByDB.getId().equals(artileType.getId());
            }
            //值为0： OK的
            //值不为0： 1个       拿到这个对象的值，和前端的id进行匹配，
            // 如果相等ok，如果不相等，就不ok
        }
    }*/

}
